home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1998 July / EnigmA AMIGA RUN 29 (1998)(G.R. Edizioni)(IT)[!][issue 1998-07 & 08].iso / earcd / phase5 / lha-ppc / src / crcio.c < prev    next >
C/C++ Source or Header  |  1997-12-04  |  6KB  |  318 lines

  1. /***********************************************************
  2.     crcio.c -- input/output
  3. ***********************************************************/
  4. #ifdef __STDC__
  5. #include <stdlib.h>
  6. #include <stdarg.h>
  7. #endif
  8.  
  9. #include <errno.h>
  10. #include "slidehuf.h"
  11. #include "intrface.h"
  12.  
  13. extern int text_mode;
  14. extern int prev_char;
  15. extern int verify_mode;
  16. #ifdef EUC
  17. extern int euc_mode;
  18. extern int generic_format;
  19. #endif
  20.  
  21. long reading_size;
  22.  
  23. #define CRCPOLY  0xA001  /* CRC-16 */
  24. #define UPDATE_CRC(c) \
  25.     crc = crctable[(crc ^ (c)) & 0xFF] ^ (crc >> CHAR_BIT)
  26.  
  27. FILE *infile, *outfile;
  28. unsigned short crc, bitbuf;
  29.  
  30. static unsigned short crctable[UCHAR_MAX + 1];
  31. static unsigned char  subbitbuf, bitcount;
  32. #ifdef EUC
  33. static int putc_euc_cache;
  34. #endif
  35. static int getc_euc_cache;
  36.  
  37. void make_crctable(void)
  38. {
  39.     unsigned int i, j, r;
  40.  
  41.     for (i = 0; i <= UCHAR_MAX; i++) {
  42.         r = i;
  43.         for (j = 0; j < CHAR_BIT; j++)
  44.             if (r & 1) r = (r >> 1) ^ CRCPOLY;
  45.             else       r >>= 1;
  46.         crctable[i] = r;
  47.     }
  48. }
  49.  
  50.  
  51. #ifdef NEED_INCREMENTAL_INDICATOR
  52. extern int quiet;
  53. extern int indicator_count;
  54. extern int indicator_threshold;
  55.  
  56. static void put_indicator(long int count)
  57. {
  58.     if (!quiet && indicator_threshold)
  59.     {
  60.         while ( count > indicator_count) {
  61.             putchar ('o');
  62.             fflush (stdout);
  63.             indicator_count += indicator_threshold;
  64.         }
  65.     }
  66. }
  67. #endif
  68.  
  69. unsigned short calccrc(unsigned char *p, unsigned int n)
  70. {
  71.     reading_size += n;
  72. #ifdef NEED_INCREMENTAL_INDICATOR
  73.     put_indicator( reading_size );
  74. #endif
  75.     while (n-- > 0) UPDATE_CRC(*p++);
  76.     return crc;
  77. }
  78.  
  79. /* Shift bitbuf n bits left, read n bits */
  80. void fillbuf(unsigned char n)
  81. {
  82.   while (n > bitcount) {
  83.     n -= bitcount;
  84.     bitbuf = (bitbuf << bitcount) + (subbitbuf >> (CHAR_BIT - bitcount));
  85.     if (compsize != 0) {
  86.       compsize--;  subbitbuf = (unsigned char) getc(infile);
  87.     } else subbitbuf = 0;
  88.     bitcount = CHAR_BIT;
  89.   }
  90.   bitcount -= n;
  91.   bitbuf = (bitbuf << n) + (subbitbuf >> (CHAR_BIT - n));
  92.   subbitbuf <<= n;
  93. }
  94.  
  95. unsigned short getbits(unsigned char n)
  96. {
  97.     unsigned short x;
  98.  
  99.     x = bitbuf >> (2 * CHAR_BIT - n);  fillbuf(n);
  100.     return x;
  101. }
  102.  
  103. /* Write rightmost n bits of x */
  104. void putcode(unsigned char n, unsigned short x)
  105. {
  106.   while (n >= bitcount) {
  107.     n -= bitcount;
  108.     subbitbuf += x >> (USHRT_BIT - bitcount);
  109.     x <<= bitcount;
  110.     if (compsize < origsize) {
  111.       if (fwrite(&subbitbuf, 1, 1, outfile) == 0)
  112.     /*    fileerror(WTERR, outfile); */
  113.     exit( errno );
  114.       compsize++;
  115.     } else unpackable = 1;
  116.     subbitbuf = 0;  bitcount = CHAR_BIT;
  117.   }
  118.   subbitbuf += x >> (USHRT_BIT - bitcount);
  119.   bitcount -= n;
  120. }
  121.  
  122. /* Write rightmost n bits of x */
  123. void putbits(unsigned char n, unsigned short x)
  124. {
  125.     x <<= USHRT_BIT - n;
  126.     while (n >= bitcount) {
  127.         n -= bitcount;
  128.         subbitbuf += x >> (USHRT_BIT - bitcount);
  129.         x <<= bitcount;
  130.         if (compsize < origsize) {
  131.             if (fwrite(&subbitbuf, 1, 1, outfile) == 0)
  132.                 /* fileerror(WTERR, outfile); */
  133.                 exit( errno );
  134.             compsize++;
  135.         } else unpackable = 1;
  136.         subbitbuf = 0;  bitcount = CHAR_BIT;
  137.     }
  138.     subbitbuf += x >> (USHRT_BIT - bitcount);
  139.     bitcount -= n;
  140. }
  141.  
  142. int fread_crc(unsigned char *p, int n, FILE *fp)
  143. {
  144.     if ( text_mode )
  145.       n = fread_txt(p, n, fp);
  146.     else
  147.       n = fread(p, 1, n, fp);
  148.     calccrc(p, n);
  149.     return n;
  150. }
  151.  
  152. void fwrite_crc(unsigned char *p, int n, FILE *fp)
  153. {
  154.   calccrc(p,n);
  155.   if ( verify_mode ) return;
  156.  
  157.   if ( fp )
  158.     {
  159.       if ( text_mode )
  160.     {
  161.       if ( fwrite_txt(p , n , fp) )
  162.         fatal_error("File write error\n");
  163.     }
  164.       else
  165.     {
  166.       if (fwrite(p, 1, n, fp) < n)
  167.         fatal_error("File write error\n");
  168.     }
  169.     }
  170. }
  171.  
  172. void init_code_cache(void)    /* called from copyfile() in util.c */
  173. {
  174. #ifdef EUC
  175.     putc_euc_cache = EOF;
  176. #endif
  177.     getc_euc_cache = EOF;
  178. }
  179.  
  180. void init_getbits(void)
  181. {
  182.     bitbuf = 0;  subbitbuf = 0;  bitcount = 0;
  183.     fillbuf(2 * CHAR_BIT);
  184. #ifdef EUC
  185.     putc_euc_cache = EOF;
  186. #endif
  187. }
  188.  
  189. void init_putbits(void)
  190. {
  191.     bitcount = CHAR_BIT;  subbitbuf = 0;
  192.     getc_euc_cache = EOF;
  193. }
  194.  
  195. #ifdef EUC
  196. void putc_euc(int c, FILE *fd)
  197. {
  198.   int d;
  199.  
  200.   if (putc_euc_cache == EOF)
  201.     {
  202.       if (!euc_mode || c < 0x81 || c > 0xFC)
  203.         {
  204.           putc(c, fd);
  205.           return;
  206.         }
  207.       if (c >= 0xA0 && c < 0xE0)
  208.         {
  209.           putc(0x8E, fd);      /* single shift */
  210.           putc(c, fd);
  211.           return;
  212.         }
  213.       putc_euc_cache = c;      /* save first byte */
  214.       return;
  215.     }
  216.   d = putc_euc_cache;
  217.   putc_euc_cache = EOF;
  218.   if (d >= 0xA0)
  219.     d -= 0xE0 - 0xA0;
  220.   if (c > 0x9E)
  221.     {
  222.       c = c - 0x9F + 0x21;
  223.       d = (d - 0x81) * 2 + 0x22;
  224.     }
  225.   else
  226.     {
  227.       if (c > 0x7E)
  228.         c --;
  229.       c -= 0x1F;
  230.       d = (d - 0x81) * 2 + 0x21;
  231.     }
  232.   putc(0x80 | d, fd);
  233.   putc(0x80 | c, fd);
  234. }
  235. #endif
  236.  
  237. int fwrite_txt(unsigned char *p, int n, FILE *fp)
  238. {
  239.   while ( --n>=0 )
  240.     {
  241.       if ( *p!='\015' && *p!='\032' )
  242.     {
  243. #ifdef EUC
  244.       putc_euc( *p , fp );
  245. #else
  246.       putc( *p , fp );
  247. #endif
  248.     }
  249.  
  250.       prev_char = *p++;
  251.     }
  252.   return ( ferror( fp ) );
  253. }
  254.  
  255. int fread_txt(unsigned char *p, int n, FILE *fp)
  256. {
  257.   int c;
  258.   int cnt = 0;
  259.  
  260.   while (cnt < n)
  261.     {
  262.       if (getc_euc_cache != EOF)
  263.         {
  264.           c = getc_euc_cache;
  265.           getc_euc_cache = EOF;
  266.         }
  267.       else
  268.         {
  269.           if ((c = fgetc(fp)) == EOF)
  270.             break;
  271.           if (c == '\n') {
  272.             getc_euc_cache = c;
  273.             c = '\r';
  274.       }
  275. #ifdef EUC
  276.       else if (euc_mode && (c == 0x8E || 0xA0 < c && c < 0xFF))
  277.         {
  278.           int d = fgetc(fp);
  279.           if (d == EOF)
  280.             {
  281.           *p++ = c;
  282.               cnt ++;
  283.               break;
  284.             }
  285.           if (c == 0x8E)    /* single shift (KANA) */
  286.             {
  287.           if ((0x20 < d && d < 0x7F) || (0xA0 < d && d < 0xFF))
  288.             c = d | 0x80;
  289.           else
  290.             getc_euc_cache = d;
  291.             }
  292.           else
  293.               {
  294.                 if (0xA0 < d && d < 0xFF)    /* if GR */
  295.                   {
  296.                     c &= 0x7F;    /* convert to MS-kanji */
  297.                     d &= 0x7F;
  298.                     if (!(c & 1))
  299.                       {
  300.                         c --;
  301.                         d += 0x7F - 0x21;
  302.                       }
  303.               if ((d += 0x40 - 0x21) > 0x7E)
  304.             d ++;
  305.               if ((c = (c >> 1) + 0x71) >= 0xA0)
  306.                 c += 0xE0 - 0xA0;
  307.                   }
  308.           getc_euc_cache = d;
  309.               }
  310.         }
  311. #endif
  312.         }
  313.       *p++ = c;
  314.       cnt ++;
  315.     }
  316.   return cnt;
  317. }
  318.